package com.jwsoft.manager.core.task.zjjy;

import com.alibaba.fastjson.JSON;
import com.bifang.engine.assistant.core.exception.AppException;
import com.bifang.engine.assistant.core.utils.SessionUtil;
import com.bifang.engine.cache.redis.RedisHelper;
import com.jwsoft.manager.common.vo.eduSyncZjDelete.EduSyncZjDeleteQueryVO;
import com.jwsoft.manager.common.vo.eduSyncZjEnroll.EduSyncZjEnrollQueryVO;
import com.jwsoft.manager.core.integration.impl.EduSyncZjDeleteIntegrationImpl;
import com.jwsoft.manager.core.integration.impl.EduSyncZjEnrollIntegrationImpl;
import com.jwsoft.manager.core.util.EduHelper;
import com.xxl.job.core.context.XxlJobHelper;
import com.xxl.job.core.handler.annotation.XxlJob;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
 * 省招生删除同步数据
 *
 * @author chenzh
 */
@Component
@Slf4j
public class SyncDeleteTaskJob {
    @Autowired
    EduSyncZjDeleteIntegrationImpl eduSyncZjDeleteIntegration;
    @Autowired
    private RedisHelper redisHelper;
    @Autowired
    private EduHelper eduHelper;

    @XxlJob("syncZjjyDeleteData")
    public void jhjyBmJobHandler() {
        XxlJobHelper.log("XXL-JOB, syncZjjyDeleteData");
        String str = XxlJobHelper.getJobParam();
        EduSyncZjDeleteQueryVO vo = JSON.parseObject(str, EduSyncZjDeleteQueryVO.class);
        String redisKey = "edu:zjSync:send:delete";
        if (redisHelper.hasKey(redisKey)) {
            throw new AppException("正在执行中");
        }
        redisHelper.set(redisKey, true, 600);
        // 开启分页
        if (vo.getPageNum() == null || vo.getPageSize() == null) {
            //如果不传入分页参数，默认最大取1000条
            vo.setPageNum(1);
            vo.setPageSize(100);
        }
        vo.setTheYear(eduHelper.thisYear());
        if (StringUtils.isBlank(vo.getApplyCountyCode())) {
            vo.setApplyCountyCode(SessionUtil.getAreaCode());
        }
        if (StringUtils.isBlank(vo.getApplyCountyCode())) {
            vo.setApplyCountyCode(eduHelper.thisAreaCode());
        }
        if (StringUtils.isBlank(vo.getSyncStatus())) {
            vo.setSyncStatus("0");
        }

        log.info("省招生删除数据同步开始");
        try {
            eduSyncZjDeleteIntegration.batchSyncDo(vo);
        } catch (Exception e) {
            redisHelper.del(redisKey);
            e.printStackTrace();
        }
        log.info("省招生删除数据同步结束");
    }
}
